;;; +python.el --- Python configs -*- lexical-binding: t -*-

;; Author: p1uxtar


;; This file is not part of GNU Emacs

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.


;;; Commentary:

;; Custom configurations for Python.

;;; Code:

;; -------- Python basic --------
(setq python-indent 4
      python-indent-guess-indent-offset-verbose nil
      python-indent-offset 4
      python-shell-buffer-name "iPython"
	  python-shell-interpreter "ipython"
	  python-shell-interpreter-args "--simple-prompt -i"
      ;; python-shell-prompt-input-regexps "In \\[[0-9]+\\]: "
      ;; python-shell-prompt-output-regexps "Out\\[[0-9]+\\]: "
      ;; python-shell-completion-setup-code "from IPython.core.completerlib import module_completion"
      ;; python-shell-completion-module-string-code "';'.join(module_completion('''%s'''))\n"
      ;; python-shell-completion-string-code "';'.join(get_ipython().Completer.all_completions('''%s'''))\n"
      )

(defun p1uxtar-run-python ()
  (interactive)
  (or (python-shell-get-process) (call-interactively 'run-python))
  (if (region-active-p)
      (python-shell-send-region (region-beginning) (region-end) t)
    (python-shell-send-buffer t)))

(add-hook 'python-mode-hook
          (lambda ()
            (require 'pyvenv) ;; then use `pyvenv-activate' to specify python
            ;; (aggressive-indent-mode -1)
	        (local-set-key (kbd "C-c C-c") 'p1uxtar-run-python)))

;; ;; -------- eglot with python-language-server[all] --------
;; (add-hook 'python-mode-hook
;; 	      '(lambda ()
;; 	         (lsp)
;; 	         (push '(company-lsp :with company-tabnine :separate)
;; 	     	       company-backends)
;; 	         (when (string-equal system-type "gnu/linux")
;; 	           (require 'pyvenv)
;; 	           (pyvenv-activate "~/miniconda2/envs/python3")
;; 	           (add-to-list 'eglot-server-programs
;; 			                '((python-mode)
;; 			                  . ("~/miniconda2/envs/python3/bin/pyls" "-v" "--tcp" "--host"
;; 				                 "localhost" "--port" :autoport)))
;; 	           (eglot-ensure))))

;; eglot & nox
(cond
 ((eq system-type 'gnu/linux)
  (add-to-list 'eglot-server-programs
	           `(python-mode . ("pyls" "-v" "--tcp" "--host"
				                "localhost" "--port" :autoport)))
  (add-hook 'python-mode-hook 'eglot-ensure))
 ((eq system-type 'windows-nt)
  (pyvenv-activate "c:/Users/Yuanchen.Xie/Anaconda3/python.exe")
  (use-package nox
    :load-path "lazycat-bucket/nox"
    :config
    (add-to-list 'nox-server-programs
	             `(python-mode . ("c:/Users/Yuanchen.Xie/Anaconda3/Scripts/pyls.exe" "-v" "--tcp" "--host"
				                  "localhost" "--port" :autoport)))
    (add-hook 'python-mode-hook 'nox-ensure))))

;; ;; -------- lsp --------
;; ;; github.com/emacs-lsp/lsp-python-ms
;; (use-package lsp-python-ms
;;   ;; Need to demand otherwise this never gets loaded.
;;   :demand t
;;   ;; But only after lsp otherwise for some cryptic emacsy reason this doesn't work properly.
;;   :after lsp python
;;   ;; :if (or *python3* *python*)
;;   :init
;;   (when (string-equal system-type "gnu/linux")
;;     (require 'pyvenv)
;;     (pyvenv-activate "~/miniconda2/envs/python3"))
;;   (when (executable-find "python3")
;;     (setq lsp-python-ms-python-executable-cmd "python3"))
;;   (setq-default lsp-pyls-plugins-pylint-enabled nil
;; 		lsp-pyls-configuration-sources ["flake8"])
;;   :hook
;;   (python-mode . (lambda ()
;; 		   (require 'lsp-python-ms)
;; 		   (lsp)
;; 		   (aggressive-indent-mode -1)))
;;   :config
;;   (setq lsp-python-ms-dir "~/ms-py-ls/output/bin/Release/linux-x64/publish/"
;;   	lsp-python-ms-executable "~/ms-py-ls/output/bin/Release/linux-x64/publish/Microsoft.Python.LanguageServer"))

;; ;; github.com/xhcoding/ms-python
;; (use-package ms-python
;;   :load-path "site-lisp"
;;   :init
;;   (use-package lsp-mode
;;     :ensure t)
;;   (use-package lsp-ui
;;     :ensure t
;;     :config
;;     (setq lsp-ui-doc-max-height 20
;; 	  lsp-ui-doc-max-width 50
;; 	  lsp-ui-sideline-ignore-duplicate t
;; 	  lsp-ui-peek-always-show t))
;;   (use-package company-lsp
;;     :ensure t
;;     :commands
;;     (company-lsp))
;;   (use-package company-box
;;     :ensure t
;;     :hook (company-mode . company-box-mode))
;;   :hook
;;   (python-mode . lsp)
;;   :config
;;   (setq ms-python-server-install-dir "~/.emacs.d/site-lisp/"))

(provide '+python)
;;; +python.el ends here
